# coding:utf-8
import requests
import pandas as pd
import re
import json
from lxml import  etree

header = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Host": "fund.eastmoney.com",
    "Referer": "http://fund.eastmoney.com/data/fundranking.html",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
}

scs = {"日增长率":"rzdf","近1周":"zzf","近1月":"1yzf","近3月":"3yzf","近6月":"6yzf","近1年":"1nzf","近3年":"3nzf","今年来":"jnzf","成立来":"lnzf"}
gp_param = {
'op': 'ph',
'dt': 'kf',
'ft': 'all',
'rs': '',
'gs': 0,
'sc': 'rzdf',
'st': 'desc',
'sd': '2021-11-16',
'ed': '2021-11-16',
'qdii': '',
'tabSubtype': ',,,,,',
'pi': 1,
'pn': 50,
'dx': 1,
'v': 0.12296642077721809
}

etf_param = {
'op': 'ph',
'dt': 'fb',
'ft': 'ct',
'rs': '',
'gs': 0,
'sc': 'rzdf',
'st': 'desc',
'sd': '2021-11-15',
'ed': '2021-11-15',
'qdii': '',
'tabSubtype': ',,,,,',
'pi': 1,
'pn': 50,
'v': 0.12296642077721809
}

def getgprank(scname):
    url = 'http://fund.eastmoney.com/data/rankhandler.aspx'
    gp_param['sc'] = scs[scname]
    resp = requests.get(url, headers=header, params=gp_param)
    #print(resp.text)
    content = re.findall("\".*?\",", resp.text)
    #print(content)

    list = [[]]
    list.clear()
    for cnt in content:
        cnt = cnt.replace('"','')
        row = cnt.split(',')[0:16]
        #print(row)
        list.append(row)

    df = pd.DataFrame(list, columns=('基金代码','基金简称','基金内部代码','日期','单位净值','累计净值','日增长率','近1周','近1月','近3月','近6月','近1年','近2年','近3年','今年来','成立来'))
    #df2 = df.sort_values(by=scname, ascending=False)
    print(df.loc[:,['基金代码','基金简称',scname]].head(10))
    df2 = df.loc[:,['基金代码','基金简称',scname]].head(10)
    df2.to_csv(scname+"_gp.csv", encoding='gbk')

    for d in df2.values:
        getf10stock(d[0])

def getetfrank(scname):
    url = 'http://fund.eastmoney.com/data/rankhandler.aspx'
    etf_param['sc'] = scs[scname]
    resp = requests.get(url, headers=header, params=etf_param)
    print(resp.text)
    content = re.findall("\".*?\",", resp.text)
    #print(content)

    list = [[]]
    list.clear()
    for cnt in content:
        cnt = cnt.replace('"','')
        row = cnt.split(',')[0:15]
        #print(row)
        list.append(row)

    df = pd.DataFrame(list, columns=('基金代码','基金简称','基金内部代码','日期','单位净值','累计净值','近1周','近1月','近3月','近6月','近1年','近2年','近3年','今年来','成立来'))
    #df2 = df.sort_values(by=scname, ascending=False)
    print(df.loc[:,['基金代码','基金简称',scname]].head(10))
    df2 = df.loc[:,['基金代码','基金简称',scname]].head(10)
    df2.to_csv(scname+"_etf.csv", encoding='gbk')

def getf10stock(code):
    url = f"https://fund.eastmoney.com/{code}.html"
    resp = requests.get(url, headers=header)
    resp.encoding = 'utf-8'
    #print(resp.text)
    html = etree.HTML(resp.text)
    divs = html.xpath('//*[@id="position_shares"]/div[1]/table/tr')
    print(divs)
    columns = ['股票代码','股票名称','持仓占比','涨跌幅']
    list = [[]]
    list.clear()

    if divs:
        for div in divs:
            name = div.xpath('./td[1]/a/text()')
            weight = div.xpath('./td[2]/text()')
            change_rate = div.xpath('./td[3]/span/text()')
            stockcode = div.xpath('./td[3]/@stockcode')
            if name:
                print(f'stockcode:{stockcode},name:{name},weight:{weight},change_rate:{change_rate}')
                row = [stockcode[0],name[0],weight[0],change_rate[0]]
                list.append(row)

    df = pd.DataFrame(list, columns=columns)
    df.to_csv(f'{code}.csv', encoding='utf-8')
    print(df)

if __name__ == '__main__':
    getgprank('日增长率')
    #getgprank('近1周')
    #getgprank('近1月')

    #getetfrank('近1周')
    #getetfrank('近1月')
    #getetfrank('近3月')

